package com.gjzt.business.media.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.gjzt.business.media.service.MediaLineService;
import com.gjzt.business.media.service.MediaResourceMarksService;

@Controller
@RequestMapping(value = "/media/line")
public class MediaLineController {

    @Autowired
    private MediaLineService mediaLineService;
    
    @Autowired
    private MediaResourceMarksService mediaResourceMarksService;
    
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject queryMediaLines(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
    	 return Response.response(mediaLineService.findMediaLine(reqBean));
    }
    
    /**
     * 查询线路环境标签
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/{lineId}/marks")
    @ResponseBody
    public JSONObject queryMarksOfBus(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean,@PathVariable String lineId) {
        return Response.response(mediaResourceMarksService.queryMarksOfLine(lineId));
    }
    
    
    /**
     * 保存线路信息
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject updateMediaLine(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	mediaLineService.saveMediaLine(reqBean);
        return Response.responseSuccess();
    }
    
    public static long lastRefreshLinesTimes = 0;
    /**
     * 刷新线路
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/refresh")
    @ResponseBody
    public JSONObject refreshLines(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	int minite = 1;
    	if(System.currentTimeMillis() - lastRefreshLinesTimes >= minite*60*1000){
    		lastRefreshLinesTimes = System.currentTimeMillis();
    		mediaLineService.syncLines();
            return Response.responseSuccess();
    	}else{
    		long i = minite - (System.currentTimeMillis() - lastRefreshLinesTimes)/60/1000;
    		return Response.responseError("请在" + i + "分钟后执行此操作");
    	}
    }
   
    
}